package com.neusoft.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.neusoft.entity.Dept;
import com.neusoft.entity.Emp;

public class test {

	SqlSessionFactory sqlSessionFactory=null;
	SqlSession sqlSession=null;
	
	@Before
	public void init() throws IOException{
		String resource="mybatis-config.xml";
		InputStream inputStream=Resources.getResourceAsStream(resource);
		sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
		sqlSession=sqlSessionFactory.openSession();
	}
	
	@After
	public void destory(){
		sqlSession.close();
	}
	
	@Test
	public void testgetEmpByDeptno(){
		List<Emp> list= sqlSession.selectList("selectDept", 1);
		for(Emp emp:list){
			System.out.println(emp.getEname());
		}
	}
	
	@Test
	public void testinsertEmp(){
		Emp emp = new Emp(3,1,"jkl",4300,"oo",new Date());
		sqlSession.insert("insertEmp", emp);
		sqlSession.commit();
		System.out.println("success");
	}
	
	@Test
	public void testgetEmpbyEmpno(){
		Emp emp = sqlSession.selectOne("selectEmp", 1);
		System.out.println(emp.getEname());
	}
	
	@Test
	public void testmoveEmp(){
		Emp emp = new Emp();
		emp.setEmpno(3);
		emp.setDeptno(2);
		sqlSession.update("updateEmp", emp);
		sqlSession.commit();
		System.out.println("success");
	}
	
	@Test
	public void testupdateTotalSal(){
		Dept dept = new Dept();
		dept.setDeptno(1);
		dept.setTotalSal(22000);
		sqlSession.update("updateTotalSal", dept);
		sqlSession.commit();
		System.out.println("success");
	}
	
//	@Test
//	public void getTotal(){
//		double total=sqlSession.selectOne("getTotal", 1);
//		System.out.println(total);
//	}
	
}
